package cm.tl.pms.sys.service.impl;

import cm.tl.pms.sys.controller.req.UserInfoReq;
import cm.tl.pms.sys.entity.SysDept;
import cm.tl.pms.sys.entity.SysUser;
import cm.tl.pms.sys.entity.SysUserRole;
import cm.tl.pms.sys.mapper.SysDeptMapper;
import cm.tl.pms.sys.mapper.SysUserMapper;
import cm.tl.pms.sys.mapper.SysUserRoleMapper;
import cm.tl.pms.sys.pojo.UserDataPower;
import cm.tl.pms.sys.service.ISysDeptService;
import cm.tl.pms.sys.service.ISysUserService;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author laker
 * @since 2021-08-05
 */
@Service
public class SysUserServiceImpl extends MPJBaseServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    SysDeptMapper deptMapper;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    ISysDeptService sysDeptService;

    @Override
    public SysUser getUserInfo() {
        SysUser user = this.getById(StpUtil.getLoginIdAsLong());
        long roleIdByUserId = this.getRoleIdByUserId(StpUtil.getLoginIdAsLong());
        SysDept dept = sysDeptService.getById(user.getDeptId());
        if (dept != null) {
            user.setDeptName(dept.getDeptName());
        }
        user.setRoleIds(String.valueOf(roleIdByUserId));
        user.setRoleId(roleIdByUserId);
        return user;
    }

    @Override
    public SysUser getUserAndDeptById(Long userId) {
        SysUser user = this.getById(userId);
        if (user == null) {
            return null;
        }
        user.setDept(deptMapper.selectById(user.getDeptId()));
        return user;
    }

    @Override
    public List<UserDataPower> getUserDataPowers(Long userId) {
        return this.getBaseMapper().getUserDataPowers(userId);
    }

    @Override
    public long getRoleIdByUserId(Long userId) {
        return sysUserRoleMapper.selectOne(Wrappers.lambdaQuery(SysUserRole.class).eq(SysUserRole::getUserId, userId)).getRoleId();
    }

    @Override
    public List<SysUser> getUsersBydeptIdAndRoleId(long deptId, int roleId) {
        return baseMapper.getUsersBydeptIdAndRoleId(deptId, roleId);
    }

    @Override
    public boolean verifyPhone(String phone) {
        return SqlHelper.retBool(this.count(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getPhone, phone)
        ));
    }

    @Override
    public boolean verifyUserName(String username) {
        return SqlHelper.retBool(this.count(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getUserName, username)
        ));
    }

    @Override
    public boolean passwordReset(String username, String pass) {
        return this.update(Wrappers.lambdaUpdate(SysUser.class)
                .set(SysUser::getPassword, SecureUtil.sha256(pass))
                .set(SysUser::getLoginFirst, "0")
                .eq(SysUser::getUserName, username)
        );
    }

    @Override
    public SysUser signIn(String username, Long deptId, String phone) {
        SysUser user = new SysUser();
        user.setUserName(username);
        user.setPassword(SecureUtil.sha256("123456"));
        user.setNickName(username);
        user.setDeptId(deptId);
        user.setPhone(phone);
        this.save(user);
        return user;
    }

    @Override
    public SysUser signInEmail(String username, Long deptId, String email) {
        SysUser user = new SysUser();
        user.setUserName(username);
        user.setPassword(SecureUtil.sha256("123456"));
        user.setNickName(username);
        user.setDeptId(deptId);
        user.setEmail(email);
        this.save(user);
        return user;
    }

    @Override
    public boolean modifyUserInfo(UserInfoReq userInfoReq) {
        long userId = StpUtil.getLoginIdAsLong();
        return this.update(Wrappers.lambdaUpdate(SysUser.class)
                .set(StrUtil.isNotBlank(userInfoReq.getPhone()), SysUser::getPhone, userInfoReq.getPhone())
                .set(StrUtil.isNotBlank(userInfoReq.getAvatar()), SysUser::getAvatar, userInfoReq.getAvatar())
                .set(ObjectUtil.isNotEmpty(userInfoReq.getSex()), SysUser::getSex, userInfoReq.getSex())
                .set(StrUtil.isNotBlank(userInfoReq.getEmail()), SysUser::getEmail, userInfoReq.getEmail())
                .eq(SysUser::getUserId, userId)
        );
    }

    @Override
    public boolean verifyUserNameAndPhone(String username, String phone) {
        return SqlHelper.retBool(this.count(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getUserName, username)
                .eq(SysUser::getPhone, phone)
        ));
    }

    @Override
    public boolean verifyUserNameAndEmail(String username, String email) {
        return SqlHelper.retBool(this.count(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getUserName, username)
                .eq(SysUser::getEmail, email)
        ));
    }

    @Override
    public boolean verifyEmail(String email) {
        return SqlHelper.retBool(this.count(Wrappers.lambdaQuery(SysUser.class)
                .eq(SysUser::getEmail, email)
        ));
    }

}
